#include "CInput.h"

namespace Nervus {
	namespace Core {
		Input::Input(HWND* hWnd){
			mhWnd = hWnd;
			//mWndRect = wndRect;
			ZeroMemory(&mKeyState, sizeof(mKeyState));
			ZeroMemory(&mPrevKeyState, sizeof(mPrevKeyState));
			mMouseWheel = 0;
		}
		//-----------------------------------------------------------
		Input::~Input(){
			//mhWnd = 0;
		}
		//-----------------------------------------------------------
		void Input::updateInput(){


			if (GetForegroundWindow() == *mhWnd){
				GetCursorPos(&mMousePos);
				ScreenToClient(*mhWnd, &mMousePos);
				GetClientRect(*mhWnd, &mWndRect);

				if (mMousePos.x < 0 || mMousePos.x > mWndRect.right ||
					mMousePos.y < 0 || mMousePos.y > mWndRect.bottom) {
					mMousePos.x = -1;
					mMousePos.y = -1;
				}

				memcpy(&mPrevKeyState, &mKeyState, sizeof(mKeyState));



				for (u32 i = 0; i < NV_KEYSNUMBER; i++){
					mKeyState[i] = GetAsyncKeyState(i);
				}
			}
			else {
				mMousePos.x = -1;
				mMousePos.y = -1;

				for (u32 i = 0; i < NV_KEYSNUMBER; i++){
					mKeyState[i] = 0;
				}
			}
		}
		//-----------------------------------------------------------
		void Input::wheelMoved(i32 delta){
			mMouseWheel = delta;
		}
		//-----------------------------------------------------------
		//-----------------------------------------------------------
		D3DXVECTOR2 Input::getMousePos(){
			return D3DXVECTOR2((float)mMousePos.x, (float)mMousePos.y);
		}
		//-----------------------------------------------------------
		bool Input::isKeyDown(u32 keyCode){
			if (mKeyState[keyCode]){
				return true;
			}
			return false;
		}
		//-----------------------------------------------------------
		bool Input::isKeyUnpressed(u32 keyCode){
			if (mPrevKeyState[keyCode] && !mKeyState[keyCode]){
				return true;
			}
			return false;
		}
		//-----------------------------------------------------------
		bool Input::isKeyPressed(u32 keyCode){
			if (!mPrevKeyState[keyCode] && mKeyState[keyCode]){
				return true;
			}
			return false;
		}
		//-----------------------------------------------------------
		bool Input::isWheelMovedUp(){
			if (mMouseWheel > 0){
				mMouseWheel = 0;
				return true;
			}
			return false;
		}
		//-----------------------------------------------------------
		bool Input::isWheelMovedDown(){
			if (mMouseWheel < 0){
				mMouseWheel = 0;
				return true;
			}
			return false;
		}
	}
}